package graphic.dialogs;

import graphic.ConnectionPanel;
import graphic.MainFrame;
import graphic.mycomponents.TextChecked;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utility.GraphicUtilsFactory;
import core.Client;
import core.Configuration;

public class ClientConfigurationDialog extends AbstractDialog
{
	TextChecked mainPath;
	
	public ClientConfigurationDialog()
	{	
		super("First Configuration", "Configure", 300, 420);	
	}
	
	@Override
	public void createPanel() 
	{
		super.setPanel(new JPanel(new GridBagLayout()));		
		JPanel panel = super.getPanel(); 
		
		this.createFields();
		
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 10, 5, 10);
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		panel.add(new JLabel("Client First Configuration"), c);

		c.gridx = 0;
		c.gridy = 1;
		panel.add(this.mainPath, c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		panel.add(super.getOkButton(), c);	
	
		c.gridx = 1;
		c.gridy = 2;
		panel.add(super.getExitButton(), c);	
	}

	public void createFields()
	{
		this.mainPath = new TextChecked("", TextChecked.WRITABLE_FOLDER, true, false, null);

		GraphicUtilsFactory.setLobbyTextFieldParameters(this.mainPath, "Client Main Path");
	}

	@Override
	public void exitFunction()
	{
		MainFrame.getInstance().showMessage("Client must be configured to be used!", JOptionPane.WARNING_MESSAGE);
		MainFrame.getInstance().close();		
	}
	
	@Override
	public void okFunction()
	{
		Configuration.createDB();
		Configuration.setClientMainPath(this.mainPath.getText());
		super.dispose();
		MainFrame.getInstance().showMessage("Client Configured!", JOptionPane.INFORMATION_MESSAGE);
		
		ConnectionPanel.getInstance().logining();
	}

	@Override
	public boolean checkOkFunction() {
		if(!this.mainPath.isChecked())
		{
			JOptionPane.showMessageDialog(this, "Please check you have filled correctly the main path field!", "Error configuring the client!", JOptionPane.ERROR_MESSAGE);
			return false;
		}

		return true;
	}

}